﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace CrashSimul8or
{
    public sealed partial class PauseState : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch;

        Menu menu;

        private void buttonResume()
        {
            StateManager.currentState = States.PLAYGAME;
        }

        private void buttonOptions()
        {
            StateManager.currentState = States.PAUSEOPTIONS;
        }

        private void buttonMainMenu()
        {
            Game.Components.RemoveAt(Game.Components.Count-1);
            StateManager.currentState = States.MAINMENU;
            Game.Components.Add(new PlayGameState(Game, spriteBatch));
            if (GameVariables.music)
            {
                MediaPlayer.Play(ResourceManager.getSong("menuSong"));
            }
        }

        public PauseState(Game game, SpriteBatch spriteBatch)
            : base(game)
        {
            this.spriteBatch = spriteBatch;

            ButtonContainer buttons = new ButtonContainer();

            buttons.AddButton(ResourceManager.getTexture("buttonResume"),
                                new ButtonEvent(buttonResume),
                                Color.White,
                                Color.Orange,
                                Color.Red);

            buttons.AddButton(ResourceManager.getTexture("buttonOptions"),
                                new ButtonEvent(buttonOptions),
                                Color.White,
                                Color.Orange,
                                Color.Red);

            buttons.AddButton(ResourceManager.getTexture("buttonMainMenu"),
                                new ButtonEvent(buttonMainMenu),
                                Color.White,
                                Color.Orange,
                                Color.Red);

            menu = new Menu(game, buttons, 297, 52);
        }

        public sealed override void Update(GameTime gameTime)
        {
            if (StateManager.currentState == States.PAUSE)
            {
                menu.Update(gameTime);
            }
        }

        public sealed override void Draw(GameTime gameTime)
        {
            if (StateManager.currentState == States.PAUSE)
            {
                spriteBatch.Begin();
                spriteBatch.Draw(ResourceManager.getTexture("Loading"), Game.GraphicsDevice.Viewport.TitleSafeArea, Color.White);
                spriteBatch.DrawString(ResourceManager.getFont("JingJing"), "Pause...", new Vector2(50, Game.GraphicsDevice.Viewport.Height - 120), Color.Orange);
                spriteBatch.End();
                menu.Draw(spriteBatch);
            }
        }
    }
}